package com.itboye.mengwu.luntan.comment.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;

public class GridViewAdapter extends BaseAdapter {

	Context act;
	/**
	 * 图片集列表
	 */
	List<String> dataList=new ArrayList<String>();

	public GridViewAdapter(Context act, List<String> list) {
		this.act = act;
		dataList.clear();
		dataList = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		int count = 0;
		if (dataList != null) {
			count = dataList.size();
		}
		return count+1;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	class Holder {
		private ImageView iv;
		private ImageView selected;
		private TextView name;
		private TextView count;
		private ImageView image_delete;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		Holder holder;
		if (arg1 == null) {
			holder = new Holder();
			arg1 = View.inflate(act, R.layout.item_commentbaobei, null);
			holder.iv = (ImageView) arg1.findViewById(R.id.image);
			holder.image_delete=(ImageView)arg1.findViewById(R.id.image_delete);
			
//			holder.selected = (ImageView) arg1.findViewById(R.id.isselected);
//			holder.name = (TextView) arg1.findViewById(R.id.name);
//			holder.count = (TextView) arg1.findViewById(R.id.count);
			arg1.setTag(holder);
		} else {
			holder = (Holder) arg1.getTag();
		}
//		holder.count.setText("" + dataList.size());
//		holder.name.setText(item.bucketName);
//		holder.selected.setVisibility(View.GONE);
		holder.image_delete.setTag(arg0);
		holder.image_delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dataList.remove((int)v.getTag());
				notifyDataSetChanged();
			}
		});
		if(arg0==dataList.size()){
			holder.iv.setImageResource(R.drawable.icon_addpic_unfocused);
			holder.image_delete.setVisibility(View.GONE);
			holder.iv.setScaleType(ScaleType.FIT_XY);
			if(dataList.size()>=3){
				holder.iv.setVisibility(View.GONE);
			}
		}
		else {
			holder.iv.setTag("file://"+dataList.get(arg0));
			holder.iv.setScaleType(ScaleType.CENTER_CROP);
			XImageLoader.load("file://"+dataList.get(arg0),holder.iv);
		} 
		return arg1;
	}

}
